package org.gdstash.ui.character;

import org.gdstash.character.GDChar;
import org.gdstash.character.GDCharTeleportList;
import org.gdstash.character.GDCharUID;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.GDAbstractRiftPane;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.List;

public class GDCharRiftAct6Pane extends GDAbstractRiftPane {
    private JCheckBox cbConclaveOfTheThree;
    private JCheckBox cbKorvanPlateau;
    private JCheckBox cbTempleOfOsyr;
    private JCheckBox cbKorvanSands;
    private JCheckBox cbCairanDocks;
    private JCheckBox cbSunbaneOasis;
    private JCheckBox cbVanguardOfTheThree;
    private JCheckBox cbRuinsOfAbyd;
    private JCheckBox cbInfernalWastes;
    private JCheckBox cbKorvanCity;
    private JCheckBox cbTombOfTheEldritchSun;
    private JCheckBox cbEldritchGate;
    private JCheckBox cbLostOasis;
    private int difficulty;
    private boolean changed;

    public GDCharRiftAct6Pane(final int difficulty, final int direction) {
        this.difficulty = difficulty;
        this.changed = false;
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.cbConclaveOfTheThree).addComponent(this.cbKorvanPlateau).addComponent(this.cbTempleOfOsyr).addComponent(this.cbKorvanSands).addComponent(this.cbCairanDocks).addComponent(this.cbSunbaneOasis).addComponent(this.cbVanguardOfTheThree).addComponent(this.cbRuinsOfAbyd).addComponent(this.cbInfernalWastes).addComponent(this.cbKorvanCity).addComponent(this.cbTombOfTheEldritchSun).addComponent(this.cbEldritchGate).addComponent(this.cbLostOasis));
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.cbConclaveOfTheThree)).addGroup(layout.createParallelGroup().addComponent(this.cbKorvanPlateau)).addGroup(layout.createParallelGroup().addComponent(this.cbTempleOfOsyr)).addGroup(layout.createParallelGroup().addComponent(this.cbKorvanSands)).addGroup(layout.createParallelGroup().addComponent(this.cbCairanDocks)).addGroup(layout.createParallelGroup().addComponent(this.cbSunbaneOasis)).addGroup(layout.createParallelGroup().addComponent(this.cbVanguardOfTheThree)).addGroup(layout.createParallelGroup().addComponent(this.cbRuinsOfAbyd)).addGroup(layout.createParallelGroup().addComponent(this.cbInfernalWastes)).addGroup(layout.createParallelGroup().addComponent(this.cbKorvanCity)).addGroup(layout.createParallelGroup().addComponent(this.cbTombOfTheEldritchSun)).addGroup(layout.createParallelGroup().addComponent(this.cbEldritchGate)).addGroup(layout.createParallelGroup().addComponent(this.cbLostOasis));
        if (direction == 0) {
            layout.setHorizontalGroup(vGroup);
            layout.setVerticalGroup(hGroup);
        } else {
            layout.setHorizontalGroup(hGroup);
            layout.setVerticalGroup(vGroup);
        }
        layout.linkSize(0, this.cbConclaveOfTheThree, this.cbKorvanPlateau);
        layout.linkSize(0, this.cbConclaveOfTheThree, this.cbTempleOfOsyr);
        layout.linkSize(0, this.cbConclaveOfTheThree, this.cbKorvanSands);
        layout.linkSize(0, this.cbConclaveOfTheThree, this.cbCairanDocks);
        layout.linkSize(0, this.cbConclaveOfTheThree, this.cbSunbaneOasis);
        layout.linkSize(0, this.cbConclaveOfTheThree, this.cbVanguardOfTheThree);
        layout.linkSize(0, this.cbConclaveOfTheThree, this.cbRuinsOfAbyd);
        layout.linkSize(0, this.cbConclaveOfTheThree, this.cbInfernalWastes);
        layout.linkSize(0, this.cbConclaveOfTheThree, this.cbKorvanCity);
        layout.linkSize(0, this.cbConclaveOfTheThree, this.cbTombOfTheEldritchSun);
        layout.linkSize(0, this.cbConclaveOfTheThree, this.cbEldritchGate);
        layout.linkSize(0, this.cbConclaveOfTheThree, this.cbLostOasis);
        layout.linkSize(1, this.cbConclaveOfTheThree, this.cbKorvanPlateau);
        layout.linkSize(1, this.cbConclaveOfTheThree, this.cbTempleOfOsyr);
        layout.linkSize(1, this.cbConclaveOfTheThree, this.cbKorvanSands);
        layout.linkSize(1, this.cbConclaveOfTheThree, this.cbCairanDocks);
        layout.linkSize(1, this.cbConclaveOfTheThree, this.cbSunbaneOasis);
        layout.linkSize(1, this.cbConclaveOfTheThree, this.cbVanguardOfTheThree);
        layout.linkSize(1, this.cbConclaveOfTheThree, this.cbRuinsOfAbyd);
        layout.linkSize(1, this.cbConclaveOfTheThree, this.cbInfernalWastes);
        layout.linkSize(1, this.cbConclaveOfTheThree, this.cbKorvanCity);
        layout.linkSize(1, this.cbConclaveOfTheThree, this.cbTombOfTheEldritchSun);
        layout.linkSize(1, this.cbConclaveOfTheThree, this.cbEldritchGate);
        layout.linkSize(1, this.cbConclaveOfTheThree, this.cbLostOasis);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntCheck = UIManager.getDefaults().getFont("CheckBox.font");
        if (fntCheck == null) {
            fntCheck = fntLabel;
        }
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntCheck = fntCheck.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ACT6"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.cbConclaveOfTheThree == null) {
            (this.cbConclaveOfTheThree = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbConclaveOfTheThree.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_06_CONCLAVE"));
        this.cbConclaveOfTheThree.setFont(fntCheck);
        if (this.cbKorvanPlateau == null) {
            (this.cbKorvanPlateau = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbKorvanPlateau.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_06_KORVAN_PLATEAU"));
        this.cbKorvanPlateau.setFont(fntCheck);
        if (this.cbTempleOfOsyr == null) {
            (this.cbTempleOfOsyr = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbTempleOfOsyr.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_06_TEMPLE_OSYR"));
        this.cbTempleOfOsyr.setFont(fntCheck);
        if (this.cbKorvanSands == null) {
            (this.cbKorvanSands = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbKorvanSands.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_06_KORVAN_SANDS"));
        this.cbKorvanSands.setFont(fntCheck);
        if (this.cbCairanDocks == null) {
            (this.cbCairanDocks = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbCairanDocks.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_06_CAIRAN_DOCKS"));
        this.cbCairanDocks.setFont(fntCheck);
        if (this.cbSunbaneOasis == null) {
            (this.cbSunbaneOasis = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbSunbaneOasis.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_06_SUNBANE_OASIS"));
        this.cbSunbaneOasis.setFont(fntCheck);
        if (this.cbVanguardOfTheThree == null) {
            (this.cbVanguardOfTheThree = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbVanguardOfTheThree.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_06_VANGUARD"));
        this.cbVanguardOfTheThree.setFont(fntCheck);
        if (this.cbRuinsOfAbyd == null) {
            (this.cbRuinsOfAbyd = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbRuinsOfAbyd.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_06_RUINS_ABYD"));
        this.cbRuinsOfAbyd.setFont(fntCheck);
        if (this.cbInfernalWastes == null) {
            (this.cbInfernalWastes = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbInfernalWastes.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_06_INFERNAL_WASTES"));
        this.cbInfernalWastes.setFont(fntCheck);
        if (this.cbKorvanCity == null) {
            (this.cbKorvanCity = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbKorvanCity.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_06_KORVAN_CITY"));
        this.cbKorvanCity.setFont(fntCheck);
        if (this.cbTombOfTheEldritchSun == null) {
            (this.cbTombOfTheEldritchSun = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbTombOfTheEldritchSun.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_06_ELDRITCH_SUN"));
        this.cbTombOfTheEldritchSun.setFont(fntCheck);
        if (this.cbEldritchGate == null) {
            (this.cbEldritchGate = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbEldritchGate.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_06_ELDRITCH_GATE"));
        this.cbEldritchGate.setFont(fntCheck);
        if (this.cbLostOasis == null) {
            (this.cbLostOasis = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbLostOasis.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_06_LOST_OASIS"));
        this.cbLostOasis.setFont(fntCheck);
    }

    @Override
    public boolean hasChanged() {
        return this.changed;
    }

    @Override
    public void setChanged(final boolean changed) {
        this.changed = changed;
    }

    public void setSelected(final boolean selected) {
        this.cbConclaveOfTheThree.setSelected(selected);
        this.cbKorvanPlateau.setSelected(selected);
        this.cbTempleOfOsyr.setSelected(selected);
        this.cbKorvanSands.setSelected(selected);
        this.cbCairanDocks.setSelected(selected);
        this.cbSunbaneOasis.setSelected(selected);
        this.cbVanguardOfTheThree.setSelected(selected);
        this.cbRuinsOfAbyd.setSelected(selected);
        this.cbInfernalWastes.setSelected(selected);
        this.cbKorvanCity.setSelected(selected);
        this.cbTombOfTheEldritchSun.setSelected(selected);
        this.cbEldritchGate.setSelected(selected);
        this.cbLostOasis.setSelected(selected);
    }

    @Override
    public List<GDCharUID> getRiftList(final boolean found) {
        final List<GDCharUID> list = new LinkedList<GDCharUID>();
        if (this.cbConclaveOfTheThree.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_06_CONCLAVE_THREE);
        }
        if (this.cbKorvanPlateau.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_06_KORVAN_PLATEAU);
        }
        if (this.cbTempleOfOsyr.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_06_TEMPLE_OSYR);
        }
        if (this.cbKorvanSands.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_06_KORVAN_SANDS);
        }
        if (this.cbCairanDocks.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_06_CAIRAN_DOCKS);
        }
        if (this.cbSunbaneOasis.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_06_SUNBANE_OASIS);
        }
        if (this.cbVanguardOfTheThree.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_06_VANGUARD_THREE);
        }
        if (this.cbRuinsOfAbyd.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_06_RUINS_ABYD);
        }
        if (this.cbInfernalWastes.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_06_INFERNAL_WASTES);
        }
        if (this.cbKorvanCity.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_06_KORVAN_CITY);
        }
        if (this.cbTombOfTheEldritchSun.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_06_ELDRITCH_SUN);
        }
        if (this.cbEldritchGate.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_06_ELDRITCH_GATE);
        }
        if (this.cbLostOasis.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_06_LOST_OASIS);
        }
        return list;
    }

    @Override
    public void setChar(final GDChar gdc) {
        this.setSelected(false);
        this.changed = false;
        boolean expFG = GDStashFrame.expansionForgottenGods;
        if (gdc != null) {
            expFG = (expFG || gdc.isForgottenGodsChar());
        }
        if (expFG) {
            this.cbConclaveOfTheThree.setEnabled(true);
            this.cbKorvanPlateau.setEnabled(true);
            this.cbTempleOfOsyr.setEnabled(true);
            this.cbKorvanSands.setEnabled(true);
            this.cbCairanDocks.setEnabled(true);
            this.cbSunbaneOasis.setEnabled(true);
            this.cbVanguardOfTheThree.setEnabled(true);
            this.cbRuinsOfAbyd.setEnabled(true);
            this.cbInfernalWastes.setEnabled(true);
            this.cbKorvanCity.setEnabled(true);
            this.cbTombOfTheEldritchSun.setEnabled(true);
            this.cbEldritchGate.setEnabled(true);
            this.cbLostOasis.setEnabled(true);
        } else {
            this.cbConclaveOfTheThree.setEnabled(false);
            this.cbKorvanPlateau.setEnabled(false);
            this.cbTempleOfOsyr.setEnabled(false);
            this.cbKorvanSands.setEnabled(false);
            this.cbCairanDocks.setEnabled(false);
            this.cbSunbaneOasis.setEnabled(false);
            this.cbVanguardOfTheThree.setEnabled(false);
            this.cbRuinsOfAbyd.setEnabled(false);
            this.cbInfernalWastes.setEnabled(false);
            this.cbKorvanCity.setEnabled(false);
            this.cbTombOfTheEldritchSun.setEnabled(false);
            this.cbEldritchGate.setEnabled(false);
            this.cbLostOasis.setEnabled(false);
        }
        if (gdc == null) {
            return;
        }
        final List<GDCharUID> list = gdc.getRiftList(this.difficulty);
        for (final GDCharUID uid : list) {
            if (uid.equals(GDCharTeleportList.UID_RIFT_06_CONCLAVE_THREE)) {
                this.cbConclaveOfTheThree.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_06_KORVAN_PLATEAU)) {
                this.cbKorvanPlateau.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_06_TEMPLE_OSYR)) {
                this.cbTempleOfOsyr.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_06_KORVAN_SANDS)) {
                this.cbKorvanSands.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_06_CAIRAN_DOCKS)) {
                this.cbCairanDocks.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_06_SUNBANE_OASIS)) {
                this.cbSunbaneOasis.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_06_VANGUARD_THREE)) {
                this.cbVanguardOfTheThree.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_06_RUINS_ABYD)) {
                this.cbRuinsOfAbyd.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_06_INFERNAL_WASTES)) {
                this.cbInfernalWastes.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_06_KORVAN_CITY)) {
                this.cbKorvanCity.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_06_ELDRITCH_SUN)) {
                this.cbTombOfTheEldritchSun.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_06_ELDRITCH_GATE)) {
                this.cbEldritchGate.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_06_LOST_OASIS)) {
                this.cbLostOasis.setSelected(true);
            }
        }
    }

    private class ChangeActionListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            GDCharRiftAct6Pane.this.changed = true;
        }
    }
}
